/**  
* @Title: WebMvcConfigurer.java
* @Package com.lemon.conf
* @Description: (一句话阐述):
* @author Ran_cc
* @date 2018年5月13日
* @version V1.0  
*/
package com.lemon.conf.MyInterceptor;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.lemon.utils.StringUtil;
/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Company: </p>
 * @author Ran_cc
 * @date 2018年5月13日
 * @版本 V 1.0 
 */
@Configuration
public class WebMvcConfigurer extends WebMvcConfigurerAdapter  {
	@Override
    public void addInterceptors(InterceptorRegistry registry) {
		List<String> parList = new ArrayList<String>();
		parList.add("/error/**");
		parList.add("/static/**");
		parList.add("/auf/**");
		parList.add("/account/**");
		parList.add("/tpl/**");
		parList.add("/au/register");
		parList.add("/au/outLogin");
		parList.add("/au/validateCode");
		parList.add("/images/**");
		parList.add("/index.html");
        // 拦截所有请求，通过判断是否有 @LoginRequired 注解 决定是否需要登录 //拦截规则：除了login，其他都拦截判断
        registry.addInterceptor(authenticationInterceptor()).addPathPatterns("/**").excludePathPatterns(parList);//.excludePathPatterns("/adminUser/login");;
		registry.addInterceptor(authenticationInterceptor()).addPathPatterns("/auf/control");
        System.out.println("========== Interceptor init success "+StringUtil.DateToString(new Date(), 41)+"  ============");
        super.addInterceptors(registry);
    }
	
	/*资源处理器*/  
    public void addResourceHandlers(ResourceHandlerRegistry registry) {  
       registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");  
		registry.addResourceHandler("/static/account/**").addResourceLocations("classpath:/static/account");
    }  
	
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
		registry.addViewController("/").setViewName("redirect:/tpl/index.html");
    }
    
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(currentUserMethodArgumentResolver());
        super.addArgumentResolvers(argumentResolvers);
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(fastJsonHttpMessageConverterEx());
        super.configureMessageConverters(converters);
    }

    @Bean
    public FastJsonHttpMessageConverterEx fastJsonHttpMessageConverterEx() {
        return new FastJsonHttpMessageConverterEx();
    }

    @Bean
    public CurrentUserMethodArgumentResolver currentUserMethodArgumentResolver() {
        return new CurrentUserMethodArgumentResolver();
    }

    @Bean
    public AuthenticationInterceptor authenticationInterceptor() {
        return new AuthenticationInterceptor();
    }
    
    
    
    @Bean
    public HttpMessageConverter<String> responseBodyConverter() {
        StringHttpMessageConverter converter = new StringHttpMessageConverter(
                Charset.forName("UTF-8"));
        return converter;
    }


    @Override
    public void configureContentNegotiation(
            ContentNegotiationConfigurer configurer) {
        configurer.favorPathExtension(false);
    }
    
    
}
